package viettravel.guiandevent.map;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.maps.GeoPoint;
import viettravel.DAO.LocalMapDAO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

public class GooglemapV2Direction extends SherlockFragmentActivity implements
		OnCameraChangeListener {

	private GoogleMap mapView;
	String polylines;
	LocalMapDAO start;
	LocalMapDAO end;
	List<GeoPoint> listGeo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_draw_direction);
		// Show the Up button in the action bar.
		setupActionBar();
		setTheme(Constants.THEME);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);
		actionBar.setTitle("MAP");

		SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.drawDirectionMap);
		mapView = fm.getMap();

		Bundle extra = getIntent().getExtras();
		if (extra == null) {
			finish();
		}
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(
					extra.getString(Constants.DIRECTIONLIST));
			Log.v(Constants.TAG + "JSON", jsonObject.toString());
			polylines = jsonObject.getJSONArray(Constants.TAG_ROUTES)
					.getJSONObject(0)
					.getJSONObject(Constants.TAG_OVERVIEW_POLYLINE)
					.getString(Constants.TAG_POINTS);
			PolylineOptions rectLine = new PolylineOptions().width(3).color(
					Color.RED);

			ArrayList<LatLng> listLatLng = new ArrayList<LatLng>();
			listLatLng = GooglemapCommon.decodePoly(polylines);
			GooglemapCommon.drawDirection(mapView, rectLine, listLatLng,
					R.drawable.ic_a_position, R.drawable.ic_b_position);

			mapView.moveCamera(CameraUpdateFactory.newLatLng(listLatLng.get(0)));
			mapView.animateCamera(CameraUpdateFactory.zoomTo(12), 3000, null);
		} catch (JSONException e) {
			e.printStackTrace();
			Log.v(Constants.TAG, "Error read bundle Google map v2 direction");
		}
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onCameraChange(CameraPosition arg0) {
		// TODO Auto-generated method stub
	}
}
